package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.Assets;
import com.ruoyi.system.domain.TLandAssets;
import com.ruoyi.system.domain.TLandHouseAssets;
import com.ruoyi.system.service.IAssetsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.THouseAssets;
import com.ruoyi.system.service.ITHouseAssetsService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 房产资产Controller
 * 
 * @author ruoyi
 * @date 2024-03-13
 */
@Controller
@RequestMapping("/house/house")
public class THouseAssetsController extends BaseController
{
    private String prefix = "house/house";

    @Autowired
    private ITHouseAssetsService tHouseAssetsService;
    @Autowired
    private IAssetsService assetsService;

    @RequiresPermissions("house:house:view")
    @GetMapping()
    public String house(ModelMap mmap)
    {
        mmap.put("key","house");
        return prefix + "/house";
    }

    /**
     * 查询房产资产列表
     */
    @RequiresPermissions("house:house:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(THouseAssets tHouseAssets,String deptId)
    {
        List<THouseAssets> list = new ArrayList<>();
        Assets assets = new Assets();
        assets.setParentId(Long.valueOf(deptId));
        List<Assets> assetsList = assetsService.selectDeptList(assets);
        startPage();
        if (assetsList != null && assetsList.size()>0){
            StringBuffer sb = new StringBuffer();
            for (int i = 0;i<assetsList.size(); i++){
                Assets assets1 = assetsList.get(i);
                Long id = assets1.getDeptId();
                sb.append(id);
                if ( i != assetsList.size()-1) {
                    sb.append(",");
                }
            }
            list = tHouseAssetsService.selectTHouseAssetsListByIds(sb.toString());
        }else {
            tHouseAssets.setType(Long.valueOf(deptId));
            list = tHouseAssetsService.selectTHouseAssetsList(tHouseAssets);
        }
        return getDataTable(list);
    }

    /**
     * 导出房产资产列表
     */
    @RequiresPermissions("house:house:export")
    @Log(title = "房产资产", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(THouseAssets tHouseAssets)
    {
        List<THouseAssets> list = tHouseAssetsService.selectTHouseAssetsList(tHouseAssets);
        ExcelUtil<THouseAssets> util = new ExcelUtil<THouseAssets>(THouseAssets.class);
        return util.exportExcel(list, "房产资产数据");
    }

    /**
     * 新增房产资产
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("landHouseAssets",new TLandHouseAssets());
        return prefix + "/add";
    }

    @GetMapping("/addbyId/{deptId}")
    public String addbyId(@PathVariable("deptId")String deptId,ModelMap mmap)
    {
        Assets assets = assetsService.selectDeptById(Long.valueOf(deptId));
        String urlAdd = assets.getUrlAdd();
        TLandHouseAssets landHouseAssets = new TLandHouseAssets();
        landHouseAssets.setType(deptId);
        mmap.put("landHouseAssets",landHouseAssets);
        return urlAdd;
    }

    /**
     * 新增保存房产资产
     */
    @RequiresPermissions("house:house:add")
    @Log(title = "房产资产", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(THouseAssets tHouseAssets)
    {
        if (tHouseAssets.getId() == null){
            tHouseAssets.setId(generateRandomNumber());
        }
        return toAjax(tHouseAssetsService.insertTHouseAssets(tHouseAssets));
    }

    /**
     * 修改房产资产
     */
    @RequiresPermissions("house:house:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        THouseAssets tHouseAssets = tHouseAssetsService.selectTHouseAssetsById(id);
        mmap.put("tLandHouseAssets", tHouseAssets);
        return prefix + "/edit";
    }

    /**
     * 修改保存房产资产
     */
    @RequiresPermissions("house:house:edit")
    @Log(title = "房产资产", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(THouseAssets tHouseAssets)
    {
        int i = tHouseAssetsService.updateTHouseAssets(tHouseAssets);
        return toAjax(i);
    }

    /**
     * 删除房产资产
     */
    @RequiresPermissions("house:house:remove")
    @Log(title = "房产资产", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tHouseAssetsService.deleteTHouseAssetsByIds(ids));
    }
}
